package com.example.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.example.appcontact.R;
import com.example.component.Item003Content;
import com.example.model.Contacts;
import com.example.util.Util;

public class A003BuchMeOAdapter extends ArrayAdapter<Contacts>{

	private LayoutInflater inflater;
	private List<Contacts> mArr;
	private int layout;
	private Context context;
	
	public A003BuchMeOAdapter(Context context, int textViewResourceId,
			List<Contacts> objects) {
		super(context, textViewResourceId, objects);
		// TODO Auto-generated constructor stub
		
		this.layout = textViewResourceId;
		this.mArr = objects;
		this.context  = context;
		this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		return getViewPanel(position, convertView);
	}
	
	@Override
	public Contacts getItem(int position) {
		return super.getItem(position);
	}
	
	@Override
	public int getCount() {
		return mArr.size();
	}
	
	class ViewHolder
    {
		RelativeLayout rl;
    	ImageView img_avatar;
    	Item003Content content;
    }
	
	
	
	private View getViewPanel(int position, View convertView)
	{
		View view = convertView;
        ViewHolder mHolder = null;
        
        if (view == null) {
            // ??????????????????????????????????????????????????????????????????r???????????????????????????[??????????????????null????????????????????????V?????????????????????????????????????????????r???????????????????????????[??????????????????????????????
            view = inflater.inflate(layout, null);
            mHolder            = new ViewHolder();
            mHolder.img_avatar = (ImageView)view.findViewById(R.id.item_a003_iv_avatar);
            mHolder.content    = (Item003Content) view.findViewById(R.id.item_a003_content);
            mHolder.rl    = (RelativeLayout)view.findViewById(R.id.item_a003_layout);
            view.setTag(mHolder);
        }
        else
        {
        	mHolder = (ViewHolder) view.getTag();
        }
        
        
        if(mArr.size() > 0)
        {
        	final Contacts contact = mArr.get(position);
        	mHolder.content.setName(contact.getFirstName() + " " + contact.getLastName());
        	mHolder.content.setPhoneNumber(contact.getPhoneNumber());
        	mHolder.content.setEmail(contact.getEmail());
        	mHolder.content.setDayBirth(contact.getBirthday());
        	try{
	        	if(mHolder.img_avatar != null && contact.getAvatar() != null)
	        	{
	        		final Bitmap bmp = Util.loadContactPhoto(getContext().getContentResolver(), Long.parseLong(contact.getId()));
	        		if(bmp!=null)
	        			mHolder.img_avatar.setImageBitmap(bmp);
	        		else
	        			mHolder.img_avatar.setImageResource(R.drawable.personal_default);
	        	}else{
	        			mHolder.img_avatar.setImageResource(R.drawable.personal_default);
	        	}
        	}catch(Exception e){
    			Log.d("TAG_IMAGE","Error: " + contact.getAvatar());
    			e.printStackTrace();
    			mHolder.img_avatar.setImageResource(R.drawable.personal_default);
    		}
        }

        return view;
	}
}
